package rexsee.core.progress;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.lang.RexseeLanguage;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Html;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class RexseeProgressTextDialog implements JavascriptInterface {

	public static final String INTERFACE_NAME = "ProgressTextDialog";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeProgressTextDialog(childBrowser);
	}

	private AlertDialog mTextProgressDialog = null;

	private final Context mContext;
	private final Browser mBrowser;

	public RexseeProgressTextDialog(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
	}

	//JavaScript Interface
	public void show(final String title, final String message, final String cancelCallback) {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (mTextProgressDialog == null) {
					mTextProgressDialog =
									new AlertDialog.Builder(mContext).create();
					mTextProgressDialog.setCancelable(false);
					mTextProgressDialog.setOnKeyListener(new Dialog.OnKeyListener() {
						@Override
						public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
							if (arg1 == KeyEvent.KEYCODE_SEARCH) return true;
							else return false;
						}
					});
					LinearLayout progressLayout =
									new LinearLayout(mContext);
					progressLayout.setId(0);
					progressLayout.setOrientation(LinearLayout.VERTICAL);
					progressLayout.setPadding(0, 0, 0, 40);
					ScrollView scroller = new ScrollView(mContext);
					scroller.setId(1);
					scroller.setVerticalFadingEdgeEnabled(false);
					scroller.setHorizontalFadingEdgeEnabled(false);
					scroller.addView(progressLayout);
					LinearLayout layout = new LinearLayout(mContext);
					layout.setPadding(15, 15, 15, 15);
					layout.setMinimumHeight(800);
					layout.addView(scroller, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
					mTextProgressDialog.setView(layout);
				}
				mTextProgressDialog.setIcon(mBrowser.application.icon);
				mTextProgressDialog.setTitle(Html.fromHtml(title));
				if (cancelCallback.compareTo("") != 0) {
					mTextProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, mContext.getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							mBrowser.loadUrl(cancelCallback);
						}
					});
				}
				mTextProgressDialog.show();
				TextView newProgress = new TextView(mContext);
				newProgress.setPadding(0, 3, 0, 3);
				newProgress.setTextSize(16);
				newProgress.setTextColor(Color.WHITE);
				newProgress.setText(Html.fromHtml(message));
				LinearLayout pLayout = (LinearLayout) mTextProgressDialog.findViewById(0);
				pLayout.addView(newProgress, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
				ScrollView pScroller = (ScrollView) mTextProgressDialog.findViewById(1);
				pScroller.scrollTo(0, pLayout.getHeight());
			}
		});
	}
	public void show(final String message) {
		show(RexseeLanguage.TITLE_PROGRESS, message, "");
	}

	public void hide() {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					if (mTextProgressDialog != null) {
						mTextProgressDialog.dismiss();
						mTextProgressDialog = null;
					}
				} catch (Exception e) {
				}
			}
		});
	}

}
